/*
 * Singularity Engine
 * Copyright (C) 2010 Vyacheslav Vorobyov, All rights reserved.
 * See LICENSE for full license information.
 */

/**
 * 
 */
package org.singularity.mapred.util;

import java.io.Serializable;
import java.util.HashMap;

import org.singularity.io.DME;
import org.singularity.mapred.VDME;


/**
 * @author vjache
 *
 */
public abstract class WraperVDME<KT,VT> implements VDME<KT,VT>, Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8574722417442549681L;
	
	protected final DME<VT> _dme;

	private HashMap<Object, Object> _annots;

	protected WraperVDME(DME<VT> aDme){
		_dme = aDme;
	}

	@Override
	public DME.Code getCode() {
		return _dme.getCode();
	}

	@Override
	public VT getValue() {
		return _dme.getValue();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof VDME) || obj==null)
			return false;
		
		final VDME<KT, VT> dme = (VDME<KT, VT>)obj;
		
		final KT key = getKey();
		final VT value = getValue();
		return getCode().equals(dme.getCode()) && 
		(value !=null?value.equals(dme.getValue()):dme.getValue()==null) &&
		(key!=null?key.equals(dme.getKey()):dme.getKey()==null);
	}
	
	@Override
	public int hashCode() {
		final KT key = getKey();
		final VT value = getValue();
		return getCode().hashCode()^
		(value!=null?value.hashCode():0)^
		(key!=null?key.hashCode():0);
	}
	
	@Override
	public void putAnnotation(Object aKey, Object aValue) {
		if(_annots==null)
			_annots = new HashMap<Object, Object>(1);
		_annots.put(aKey, aValue);
	}
	
	@Override
	public Object getAnnotation(Object aKey) {
		if(_annots==null)
			return null;
		return _annots.get(aKey);
	}
}
